<%@ page language="java" contentType="text/html; charset=UTF-8"   pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!-- 屏蔽tomcat 自带的 EL表达式 -->
<%@ page isELIgnored="false" %>  
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="${ctx}/static/css/bootstrapStyle.css" media="all">
<link rel="stylesheet" type="text/css" href="${ctx}/static/bootstrap/bootstrap.css" media="all">
<script type="text/javascript" src="${ctx}/static/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="${ctx}/static/bootstrap/jquery.ztree.core.min.js"></script>
<script type="text/javascript" src="${ctx}/static/bootstrap/jquery.ztree.excheck.min.js"></script>
<script type="text/javascript" src="${ctx}/static/bootstrap/jquery.ztree.exedit.min.js"></script>
<script type="text/javascript" src="${ctx}/static/bootstrap/jquery.ztree.exhide.min.js"></script>
<script type="text/javascript" src="${ctx}/static/layer/layer.js"></script>
<script type="text/javascript" src="${ctx}/static/js/ajax.js"></script>
<style>
	 body { padding-bottom: 70px;padding-top: 5px; }
	.input{height:28px;line-height:28px;}
	.button{height:28px;line-height:28px;padding:0px 10px;}
	.navbar-fixed-bottom-cus{padding-bottom:5px;}
</style>
<SCRIPT type="text/javascript">
       
      
	function initInfo(data) {//隐藏基础角色数据
		$("#roleCode").val(data.roleCode);
		if(data !=null&&data.hasOwnProperty("status")){
			$("#savePerssion").hide();
		}
	}

	$(document).ready(function() {
				var setting = {
					async : {
						enable : true,
						url : "${ctx}/role/roleInfo/findPermission",
						otherParam: {"roleCode": parent.getData().roleCode}
					},
					check : {
						enable : true
					},
					data : {
						key : {
							name : "operatorName"
						},
						simpleData : {
							enable : true,
							idKey : "operator",
							pIdKey : "parentOperator",
							rootPId : "larry" // 用于修正根节点父节点数据，即 pIdKey 指定的属性值  
						}
					},
					edit : {
						enable : false
					},
					// 回调函数  
					callback : {
						
						onAsyncSuccess : function(event, treeId) {
							var zTree = $.fn.zTree.getZTreeObj(treeId);
							var nodeList = zTree.getNodes();
							zTree.expandNode(nodeList[0], true);
							
							var nodes = zTree.getCheckedNodes();
							for (var i = 0, l = nodes.length; i < l; i++) {
								zTree.checkNode(nodes[i], true, true);
							}
						}
					}
				};

				$.fn.zTree.init($("#treeDemo"), setting);
				var lastValue = "", nodeList = [];
				$("#role").keypress(
						function(event) {
							nodeList = [];
							var value = $("#role").val();
							var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
							var allNode = treeObj.transformToArray(treeObj
									.getNodes());
							treeObj.hideNodes(allNode);
							nodeList = treeObj.getNodesByParamFuzzy(
									"operatorName", value, null);
							nodeList = treeObj.transformToArray(nodeList);
							for ( var n in nodeList) {
								findParent(treeObj, nodeList[n]);
							}
							treeObj.showNodes(nodeList);
							if (value == "") {
								treeObj.expandAll(false);
								var nodeList1 = treeObj.getNodes();
								treeObj.expandNode(nodeList1[0], true);
							}
						});
				function findParent(treeObj, node) {
					treeObj.expandNode(node, true, false, false);
					var pNode = node.getParentNode();
					if (pNode != null) {
						nodeList.push(pNode);
						findParent(treeObj, pNode);
					}
				}
				
				
				

				$("#savePerssion").click(function(){
						var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
						var nodes = treeObj.getCheckedNodes();
						var arr=nodes.filter(function(index,ele){
						        if (!nodes[ele].isParent) {
						            return ele;
						        };
						});
						 
						$.mask.post("${ctx}/role/roleInfo/savePermission",
								{"roleCode":$("#roleCode").val(),"data":JSON.stringify(arr)}, function(data) {
									if (data.success) {
										$.mask.alert(data.msg,function(){
											parent.layer.closeAll();//关闭编辑页面
											parent.roleTable.bootstrapTable('refreshOptions', {pageNumber : 1});//调用刷新父页面
										});
									} else {
										$.mask.alert(data.msg);
									}
						});
				});
				
				
				$("#BtnCancel").click(function(){
					parent.layer.closeAll();//关闭编辑页面
				});

			});
</SCRIPT>
<title>Insert title here</title>
</head>
<body >
<div class="container-fluid ">
	<div class="row-fluid">
		<div class="span12">
		   <input type="text" class="form-control input" id="role" name="role" style="width:35%" autocomplete="off">
			<ul id="treeDemo" class="ztree"></ul>
			<footer class="footer navbar-fixed-bottom navbar-fixed-bottom-cus">
			    <div class="container"  style="text-align:right;">
						 <div  class="btn-group " role="group" >
							  <button id="savePerssion" type="button" class="btn btn-green button"> <span class="glyphicon glyphicon-floppy-save"></span> 保存</button>
							  <button id="BtnCancel" type="button" class="btn btn-red button"><span class="glyphicon glyphicon-remove"></span> 关闭</button>
						</div>
			    </div>
			</footer>
		</div>
	</div>
</div>
<input type="hidden" name="roleCode" id="roleCode"/>
</body>
</html>